/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#ifndef __SPI_H
#define __SPI_H

#include "wsn-platform.h"


/************************************************************************************
		SPI: Low level functions
************************************************************************************/
	
#define SPI_WAIT_SEND_END() 	while ( !(SPI_STATUS_REG & BIT_SET(SPI_TX_READY)) )
	
#define SPI_SEND(x)\
		do {\
			SPI_TX_REG = (x);\
			SPI_WAIT_SEND_END();\
		} while(0)
	
#define SPI_SEND_WORD(x)\
		 do {\
			SPI_TX_REG = ((x) >> 8) & 0xFF;\
			SPI_WAIT_SEND_END();\
			SPI_TX_REG = (x) & 0xFF;\
			SPI_WAIT_SEND_END();\
		} while (0)
		
#define SPI_STATUS(x) ((x) = SPI_RX_REG)
	
#define SPI_RECEIVE(x)\
		do {\
			SPI_TX_REG = 0xFF;\
			SPI_WAIT_SEND_END();\
			(x) = SPI_RX_REG & 0xFF;\
		} while(0)
	
#define SPI_RECEIVE_WORD(x)\
		 do {\
			SPI_TX_REG = 0xFF;\
			SPI_WAIT_SEND_END();\
			(x) = (SPI_RX_REG & 0xFF) << 8;\
			SPI_TX_REG = 0xFF;\
			SPI_WAIT_SEND_END();\
			(x) |= (SPI_RX_REG & 0xFF);\
		} while (0)

#endif
